package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.CustomException;
import com.itheima.common.GlobalConstant;
import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.IShoppingCartService;
import javafx.scene.layout.Background;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车控制器
 *
 * @author hua
 * @since 2022/11/7 15:18
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private IShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCartParam
     * @return
     */
     @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCartParam){
         log.info("前后端连通");
         // 1）查询当前用户的购物车中是否有此菜品或套餐的数据
         LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
         //条件一:当前登录用户id
         //对于C端来说,用户之应该能看到自己的信息
         queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentUserId());
         // 2）判断当前购物车是否存在此菜品或者套餐.
         if (shoppingCartParam.getDishId() !=null){
             queryWrapper.eq(ShoppingCart::getDishId,shoppingCartParam.getDishId());
         }else if (shoppingCartParam.getSetmealId() !=null){
             queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCartParam.getSetmealId());
         }else{
             throw new CustomException("传递的参数不正确");
         }
         ShoppingCart shoppingCart = this.shoppingCartService.getOne(queryWrapper);
         if (shoppingCart !=null){
             //3.如果存在,就在原来的数量基础上加一
             shoppingCart.setNumber(shoppingCart.getNumber()+1);
             //3.1）更新数据
             this.shoppingCartService.updateById(shoppingCart);
         }else {
             // 4）如果不存在,则添加到购物车,默认数量就是一
             shoppingCart = new ShoppingCart();
             //不存在的商品,添加到购物车,实际上就是在购物车的表中新增购物车数据,及当前时间
             //拿到前端传入的购物车的数据
            // shoppingCartParam ->shoppingCart
             BeanUtils.copyProperties(shoppingCartParam,shoppingCart);
             // 4.1）在threadlocal中取到当前用户的id,赋值给要添加的购物车数据.及当前时间。
             //当前用户id
             shoppingCart.setUserId(BaseContext.getCurrentUserId());
             //购物车的数量
             shoppingCart.setNumber(1);
             //创建时间
             shoppingCart.setCreateTime(LocalDateTime.now());
             // 4.2） 新增数据
             this.shoppingCartService.save(shoppingCart);
         }
         return R.success(shoppingCart);
    }
    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> getList(){
        //当前用户
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //当前用户的购物车
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentUserId());
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = this.shoppingCartService.list(queryWrapper);
        return R.success(list);
    }
    @DeleteMapping("/clean")
    public R<String> clean(){
        //根据用户id,清空购物车
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentUserId());
        //调用service的remove方法
        shoppingCartService.remove(queryWrapper);
        return R.success(GlobalConstant.SUCCESS);
    }
}
